package com.hp.controller;

import com.hp.annotation.Power;
import com.hp.context.Config;
import com.hp.entity.Link;
import com.hp.result.DataGridResultInfo;
import com.hp.result.ResultUtil;
import com.hp.result.SubmitResultInfo;
import com.hp.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 所有的数据返回都要求统一
 *  1.统一异常处理
 *  2.权限过滤
 *  3.规范化(国际化,异常消息通知自定义)
 *  4.不用关心前台
 */
@Power(name = "链接管理",type =0)
@RequestMapping("/links")
@Controller //restful
public class LinkController {//springmvc
    @Autowired
    private LinkService linkService;//bean spring 父子 bean controller
    /**
     * 查询所有
     * @return
     */
    @Power(name = "查询所有链接",type =1,url = "/links/querylink_result")
    @RequestMapping("/querylink_result")
    @ResponseBody
    public DataGridResultInfo querybusiness_result(
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "rows", defaultValue = "10") int rows) throws Exception {

        return linkService.findlinkList(page, rows);

    }

    /**
     * http://localhost/tk/links/querylink_result
     * 去查询页面
     * @return
     */
    @Power(name = "去查询链接页面",type =1,url = "/links/querylink")
    @GetMapping(value = "/querylink")
    public String querylink() {
        return "link/querylink";
    }

    /**
     * 去修改页面
     * @param id
     * @return
     * @throws Exception
     *  model modelMap  Map modelAndView Requeset Session Writer
     */
    @Power(name = "去修改页面链接页面",type =1,url = "/links/editlink")
    @RequestMapping("/editlink")
    public String editlink(String id,Map<String,Object> map) throws Exception{
        Link link=linkService.findById(id);
        map.put("link", link);
        return "link/editlink";
    }

    /**
     * 修改链接
     * @return
     * @throws Exception
     */
    @Power(name = "修改链接页面",type =1,url = "/links/editlinksubmit")
    @RequestMapping(value="/editlinksubmit",method=RequestMethod.POST)
    @ResponseBody
    public SubmitResultInfo editlinksubmit(Link link) throws Exception{
        linkService.updateLink(link);

        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE,666,new Object[]{"链接"}));
    }


}
